package ophiux.wechatpay.sdk.common.parse.table;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

import ophiux.wechatpay.sdk.OphiuxWeChatPaySDKException;
import ophiux.wechatpay.sdk.common.parse.IConvert;
import ophiux.wechatpay.sdk.response.DownloadBillListResponse;
import ophiux.wechatpay.sdk.response.DownloadBillResponse;

public class TableConverter implements IConvert {

	@Override
	public <T> T toEntity(Class<T> clzz, String dataSource)
			throws OphiuxWeChatPaySDKException {
		// Element rootElement = XMLUtil.getDocmentElement(dataSource);
		// dataSource=dataSource.replaceAll("`", "").replaceAll("%", "%,");
		// String[] stringbuf = dataSource.split(",");

		return buildEntityByXML(clzz, dataSource);
	}
	
	public <T> T buildEntityByXML(Class<T> clzz, final String stringbuf) throws OphiuxWeChatPaySDKException {
		DownloadBillListResponse resp = new DownloadBillListResponse();
		List<DownloadBillResponse> list=new ArrayList<DownloadBillResponse>();
		
		if(!stringbuf.contains("交易时间")) {
			//返回失败  <xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[No Bill Exist]]></return_msg><error_code><![CDATA[20002]]></error_code></xml>
//			resp.setResult_code("FAIL");
			resp.setReturn_code("FAIL");
			resp.setReturn_msg("No Bill Exist");
			resp.setErr_code("20002");
			return clzz.cast(resp);
		}
		
		System.out.println(stringbuf.substring(0,stringbuf.indexOf("费率备注")+4));
		System.out.println(stringbuf.substring(stringbuf.indexOf("费率备注")+5,stringbuf.indexOf("总交易单数"))+" ");
		System.out.println(stringbuf.substring(stringbuf.indexOf("总交易单数"),stringbuf.indexOf("申请退款总金额")+7));
		System.out.println(stringbuf.substring(stringbuf.indexOf("申请退款总金额")+8).replace(",", ""));
		
//		String[] secondTitle = stringbuf.substring(stringbuf.indexOf("总交易单数"),stringbuf.indexOf("申请退款总金额")+7).split(",");
		String[] secondContext = stringbuf.substring(stringbuf.indexOf("申请退款总金额")+8).replace(",", "").split("`");
		resp.setTotalOrderCount(secondContext[0]);
		resp.setSettlement_total_fee(secondContext[1]);
		resp.setSettlement_refund_total_fee(secondContext[2]);
		resp.setCoupon_refund_total_fee(secondContext[3]);
		resp.setService_charge(secondContext[4]);
		resp.setTotal_fee(secondContext[5]);
		resp.setRefund_total_fee(secondContext[6]);
		
		
		String[] firstTitle = stringbuf.substring(0,stringbuf.indexOf("费率备注")+4).split(",");
		String newStr = stringbuf.substring(stringbuf.indexOf("费率备注")+5,stringbuf.indexOf("总交易单数"))+" ";
		String[] firstContext = newStr.replace(",", "").split("`");
		
		System.out.println("标题列数:"+firstTitle.length);
		System.out.println("内容列数:"+firstContext.length);
		System.out.println("数据条数:"+firstContext.length/firstTitle.length);
		
		
		for (int i = 0; i < firstContext.length/firstTitle.length; i++) {
			DownloadBillResponse res = new DownloadBillResponse();
//			for (int j = i*firstTitle.length; j < (i+1) * firstTitle.length; j++) {
				int j = i * firstTitle.length;
//				System.out.println(firstTitle[j]+":"+firstContext[j]);
				if(firstTitle.length==20) {//bill_type=SUCCESS
					res.setTrade_time(firstContext[j]);
					res.setAppid(firstContext[j+1]);
					res.setMch_id(firstContext[j+2]);
					res.setSpecial_mch_id(firstContext[j+3]);
					res.setDevice_info(firstContext[j+4]);
					res.setTransaction_id(firstContext[j+5]);
					res.setOut_trade_no(firstContext[j+6]);
					res.setOpenid(firstContext[j+7]);
					res.setTrade_type(firstContext[j+8]);
					res.setTrade_state(firstContext[j+9]);
					res.setBank_type(firstContext[j+10]);
					res.setFee_type(firstContext[j+11]);
					res.setSettlement_total_fee(firstContext[j+12]);
					res.setCoupon_fee(firstContext[j+13]);
					
					res.setBody(firstContext[j+14]);
					res.setAttach(firstContext[j+15]);
					res.setService_charge(firstContext[j+16]);
					res.setRate(firstContext[j+17]);
					res.setTotal_fee(firstContext[j+18]);
					res.setRate_remark(firstContext[j+19]);
				}else if(firstTitle.length==29) {//bill_type=REFUND
					res.setTrade_time(firstContext[j]);
					res.setAppid(firstContext[j+1]);
					res.setMch_id(firstContext[j+2]);
					res.setSpecial_mch_id(firstContext[j+3]);
					res.setDevice_info(firstContext[j+4]);
					res.setTransaction_id(firstContext[j+5]);
					res.setOut_trade_no(firstContext[j+6]);
					res.setOpenid(firstContext[j+7]);
					res.setTrade_type(firstContext[j+8]);
					res.setTrade_state(firstContext[j+9]);
					res.setBank_type(firstContext[j+10]);
					res.setFee_type(firstContext[j+11]);
					res.setSettlement_total_fee(firstContext[j+12]);
					res.setCoupon_fee(firstContext[j+13]);
					
					res.setRefund_application_time(firstContext[j+14]);
					res.setRefund_success_time(firstContext[j+15]);
					res.setRefund_id(firstContext[j+16]);
					res.setOut_refund_no(firstContext[j+17]);
					res.setSettlement_refund_fee(firstContext[j+18]);
					res.setCoupon_refund_fee(firstContext[j+19]);
					res.setRefund_type(firstContext[j+20]);
					res.setRefund_status(firstContext[j+21]);
					
					res.setBody(firstContext[j+22]);
					res.setAttach(firstContext[j+23]);
					res.setService_charge(firstContext[j+24]);
					res.setRate(firstContext[j+25]);
					res.setTotal_fee(firstContext[j+26]);
					
					res.setRefund_fee(firstContext[j+27]);
					
					res.setRate_remark(firstContext[j+28]);
				}
				
				list.add(res);
//			}
		}
		resp.setDownloadBillResponse(list);
		resp.setReturn_code("SUCCESS");
		return clzz.cast(resp);
	}

	@Override
	public <T> String entityToString(T obj) throws OphiuxWeChatPaySDKException {
		return null;
	}

	@Override
	public <T> SortedMap entityToMap(T obj) throws OphiuxWeChatPaySDKException {
		return null;
	}

}
